from channels.generic.websocket import AsyncWebsocketConsumer
# 必须引入否则定时任务不会启动
import websocketapp.task

push_event = ['my_group', 'pong', 'sysinfo']
class WebSocketApp(AsyncWebsocketConsumer):
    async def connect(self):
        await self.accept()

    async def disconnect(self, code):
        await self.channel_layer.group_discard('my_group', self.channel_name)
    
    async def receive(self, text_data=None, bytes_data=None):
        if text_data in push_event:
            await self.channel_layer.group_add(text_data, self.channel_name)
        
    async def send_messages(self, event):
        await self.send(text_data=event['message'])
    